using System;
using System.Collections.Generic;
using System.Text;
using Entity;
using ECommerce.DAL;
using ECommerce.Library.DataStructure;
using System.Configuration;
using System.Web;

namespace AutoGear.BLL
{

    public class SiteSettings
    {
        static int _settingsId;
        static string _companyName;
        static string _companyAddress;
        static string _companyZip;
        static string _companyCity;
        static string _companyState;
        static string _companyCountry;
        static string _companyPhone;
        static string _companyFax;
        static string _companyEmail;
        static string _customerPrefix;
        static string _orderPrefix;
        static string _adminEmail;
        static int _stockAlertLevel;
        static bool _isLoaded = false;
        static double _minimumOrderAmount;
       // static Address _upsCompanyAddress;
        static bool _isGroundShippingFree = false;
        static double _groundShippingAmount;

        static string _homepageBannerImage;
        static string _homepageFooterImage;
        static string _homepageFooterAnchorUrl;
        static string _homepageVideoImage;

        
        public static string HomepageVideoImage
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _homepageVideoImage;
            }
            set { SiteSettings._homepageVideoImage = value; }
        }


        public static string HomepageFooterAnchorUrl
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _homepageFooterAnchorUrl;
            }
            set { SiteSettings._homepageFooterAnchorUrl = value; }
        }

        public static string HomepageFooterImage
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _homepageFooterImage;
            }
            set { SiteSettings._homepageFooterImage = value; }
        }

        public static string HomepageBannerImage
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _homepageBannerImage;
            }
            set { SiteSettings._homepageBannerImage = value; }
        }



        public static string CompanyName
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _companyName;
            }
            set { _companyName = value; }
        }
        public static string CompanyAddress
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _companyAddress;
            }
            set { _companyAddress = value; }
        }
        //public static Address UPSCompanyAddress
        //{
        //    get
        //    {
        //        if (!_isLoaded)
        //            LoadSettings();
        //        return _upsCompanyAddress;
        //    }
        //}

        public static bool IsGroundShippingFree 
        {
            get
            {
                return Convert.ToBoolean(TblSettings.GetTblSettingsBySettingsId(1).IsGroundShippingFree);
            }
            set { _isGroundShippingFree = value; }
        }
        public static double GroundShippingAmount
        {
            get
            {
                return Convert.ToDouble(TblSettings.GetTblSettingsBySettingsId(1).GroundShippingAmount);
            }
            set { _groundShippingAmount = value; }
        }
        public static double MinimumOrderAmount 
        {
            get
            {
              return Convert.ToDouble(TblSettings.GetTblSettingsBySettingsId(1).MinimumOrder);
            }
            set { _minimumOrderAmount = value; }
        }
        public static string CompanyZip
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _companyZip;
            }
            set { _companyZip = value; }
        }
        public static string CompanyCity
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _companyCity;
            }
            set { _companyCity = value; }
        }
        public static string CompanyState
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _companyState;
            }
            set { _companyState = value; }
        }
        public static string CompanyCountry
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _companyCountry;
            }
            set { _companyCountry = value; }
        }
        public static string CompanyPhone
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _companyPhone;
            }
            set { _companyPhone = value; }
        }
        public static string CompanyFax
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _companyFax;
            }
            set { _companyFax = value; }
        }

        public static string CompanyEmail
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _companyEmail;
            }
            set { _companyEmail = value; }
        }
        public static string CustomerPrefix
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _customerPrefix;
            }
            set { _customerPrefix = value; }
        }
        public static string OrderPrefix
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _orderPrefix;
            }
            set { _orderPrefix = value; }
        }
        public static string AdminEmail
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _adminEmail;
            }
            set { _adminEmail = value; }
        }

        public static int StockAlertLevel
        {
            get
            {
                if (!_isLoaded)
                    LoadSettings();
                return _stockAlertLevel;
            }
            set { _stockAlertLevel = value; }
        }



        public static string SiteName
        {
            //get { return "Polana: A Polish Experience"; }
            get { return TblSettings.GetTblSettingsBySettingsId(1).CompanyName.ToString();}
        }

        public static string SiteBaseUrl
        {
            get 
            { 
                //return ConfigurationManager.AppSettings["RootPath"].ToString(); 
                string orginalUrl = HttpContext.Current.Request.Url.AbsoluteUri;
                if (HttpContext.Current.Request.Url.Query.Length > 0)
                    orginalUrl = orginalUrl.Replace(HttpContext.Current.Request.Url.Query, string.Empty);

                return orginalUrl.Replace(HttpContext.Current.Request.Url.AbsolutePath, string.Empty) + ((HttpContext.Current.Request.ApplicationPath == "/" ? "" : HttpContext.Current.Request.ApplicationPath)) + '/';
            }

        }
        public static string NoImageUrl
        {
            get { return SiteBaseUrl + "App_Style/images/no-image.png"; }
        }

        public static string NoImageSmallThumbnailUrl
        {
            get { return SiteBaseUrl + "App_Style/images/no-image-small-thumb.png"; }
        }

        public static string NoImageLargeThumbnailUrl
        {
            get { return SiteBaseUrl + "App_Style/images/no-image.png"; }
        }

        public static string NoImageDetailSizeUrl
        {
            get { return "~/App_Style/images/no-image.png"; }
        }

        public static string NoImageMaxSizeUrl
        {
            get { return SiteBaseUrl + "App_Style/images/no-image.png"; }
        }
        public static string NoBannerImageUrl
        {
            get { return SiteBaseUrl + "App_Style/images/default-banner.png"; }
        }

        public static string SiteLogo
        {
            get { return SiteBaseUrl + "/App_Style/images/header-logo-admin.png"; } 
        }
        public static string SiteLogoMain
        {
            get { return SiteBaseUrl + "/App_Style/images/header-logo.png"; }
        }
        public static string EmailServer
        {
            get { return "smtp.emailsrvr.com"; }
        }
        public static int EmailPort
        {
            get { return 25; }
        }
        public static string EmailUserName
        {
            //get { return "noreply@desme.com"; }
            get { return TblSettings.GetTblSettingsBySettingsId(1).CompanyEmail.ToString(); }
        }
        public static string EmailPassword
        {
            get { return "d3sme0nline"; }
        }
        public static string EmailAdminAddress
        {
            //get { return "Pearlsales@addapearl.com"; }
            get { return TblSettings.GetTblSettingsBySettingsId(1).AdminEmail.ToString(); }
        }
        public static string EmailSupportAddress
        {
            //get { return "Pearlsales@addapearl.com"; }
            get { return TblSettings.GetTblSettingsBySettingsId(1).AdminEmail.ToString(); }
        }
        public static int CookieTimeOut
        {
            get { return 1; }
        }

        private static void LoadSettings()
        {
            ECommerce.DAL.CustomCollection<TblSettings> tmpSettings = TblSettings.GetAllTblSettings();
            TblSettings tblSettings;
            if (tmpSettings.Count > 0)
            {
                tblSettings = tmpSettings[0];
            }
            else
            {
                tblSettings = new TblSettings();
            }
            _settingsId = tblSettings.SettingsId;
            _companyName = tblSettings.CompanyName;
            _companyAddress = tblSettings.CompanyAddress;
            _companyZip = tblSettings.CompanyZip;
            _companyCity = tblSettings.CompanyCity;
            _companyState = tblSettings.CompanyState;
            _companyCountry = tblSettings.CompanyCountry;
            _companyPhone = tblSettings.CompanyPhone;
            _companyFax = tblSettings.CompanyFax;
            _companyEmail = tblSettings.CompanyEmail;
            _customerPrefix = tblSettings.CustomerPrefix;
            _orderPrefix = tblSettings.OrederPrefix;
            _adminEmail = tblSettings.AdminEmail;
            _stockAlertLevel = tblSettings.StockAlertLevel;
            _minimumOrderAmount =Convert.ToDouble(tblSettings.MinimumOrder);
            _groundShippingAmount = Convert.ToDouble(tblSettings.GroundShippingAmount);

            _homepageBannerImage = tblSettings.HomePageBannerImage;
            _homepageFooterImage = tblSettings.HomePageFooterImage;
            _homepageVideoImage = tblSettings.HomePageVideoImage;
            _homepageFooterAnchorUrl = tblSettings.FooterImageAnchorUrl;

            //_upsCompanyAddress.AddressCountry = _companyCountry;
            //_upsCompanyAddress.AddressLine1 = _companyAddress;
            //_upsCompanyAddress.ZipCode = _companyZip;
            //_upsCompanyAddress.AddressState = _companyState;
            //_upsCompanyAddress.City = _companyCity;
            //_upsCompanyAddress.EmailAddress = _companyEmail;
            //_upsCompanyAddress.TelephoneNumber = _companyPhone;
            _isGroundShippingFree = IsGroundShippingFree;
            _isLoaded = true;

        }

        public static void Save()
        {
            TblSettings tmpSettings;
            if (_settingsId > TblSettings.GetSettingsIdMinValue)
            {
                tmpSettings = TblSettings.GetTblSettingsBySettingsId(_settingsId);
            }
            else
            {
                tmpSettings = new TblSettings();
            }
            tmpSettings.CompanyName = _companyName;
            tmpSettings.CompanyAddress = _companyAddress;
            tmpSettings.CompanyZip = _companyZip;
            tmpSettings.CompanyCity = _companyCity;
            tmpSettings.CompanyState = _companyState;
            tmpSettings.CompanyCountry = _companyCountry;
            tmpSettings.CompanyPhone = _companyPhone;
            tmpSettings.CompanyFax = _companyFax;
            tmpSettings.CustomerPrefix = _customerPrefix;
            tmpSettings.OrederPrefix = _orderPrefix;
            tmpSettings.AdminEmail = _adminEmail;
            tmpSettings.StockAlertLevel = _stockAlertLevel;
            tmpSettings.MinimumOrder =Convert.ToDecimal(_minimumOrderAmount);
            tmpSettings.IsGroundShippingFree = _isGroundShippingFree;
            tmpSettings.GroundShippingAmount =Convert.ToDecimal(_groundShippingAmount);
            tmpSettings.HomePageBannerImage = _homepageBannerImage;
            tmpSettings.HomePageFooterImage = _homepageFooterImage;
            tmpSettings.HomePageVideoImage = _homepageVideoImage;
            tmpSettings.FooterImageAnchorUrl = _homepageFooterAnchorUrl;
            tmpSettings.Save();

        }
    }
}
